package com.SouthernWall_404.Age_Capitalism.common.World.Items;

import com.SouthernWall_404.Age_Capitalism.common.API.Info.CoinInfo;
import com.SouthernWall_404.Age_Capitalism.common.API.InventoryMethod;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoneyItem extends Item {

    public float value=0;
    public List<Component> tooltip=new ArrayList<>();
    public MoneyItem(Properties properties,float value) {
        this(properties,value,Component.empty());
    }

    public MoneyItem(Properties properties, float value, Component component) {
        super(properties);
        this.value=value;

        this.tooltip.add(component);

        CoinInfo.register(this,value);
    }

    public MoneyItem(Properties properties, float value, List<Component> component) {
        super(properties);
        this.value=value;

        this.tooltip=component;

        CoinInfo.register(this,value);
    }
    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
        ItemStack itemInHand = player.getItemInHand(hand);

        if (itemInHand.getItem() instanceof MoneyItem moneyItem)
        {
            float value= moneyItem.value;
            if(player.isShiftKeyDown())
            {
                MoneyItem toGiveMoney=CoinInfo.coins_map.get(value*16);

                if(toGiveMoney!=null)
                {
                    int amount=itemInHand.getCount();
                    int toGainAmount=amount/16;
                    int toLeftAmount=amount%16;


                    player.setItemInHand(hand,ItemStack.EMPTY);
                    player.setItemInHand(hand, itemInHand.copyWithCount(toLeftAmount));
                    // 创建要给予的货币堆栈
                    ItemStack toGiveStack = new ItemStack(toGiveMoney, toGainAmount);

                    // 使用API方法添加物品到背包或丢出
                    InventoryMethod.addItemToInventoryOrDropAll(player, toGiveStack);

                }
            }
            else
            {
                // 向下拆分：将1个高面值货币拆分为16个低面值货币
                MoneyItem toGiveMoney=CoinInfo.coins_map.get(value/16);

                if(toGiveMoney!=null) {
                    int amount = itemInHand.getCount();
                    int toGainAmount = amount * 16;

                    // 清空手中的货币
                    player.setItemInHand(hand, ItemStack.EMPTY);

                    if (toGainAmount > 0) {
                        // 处理可能超过最大堆叠数的情况
                        int maxStackSize = toGiveMoney.getMaxStackSize();

                        // 计算需要多少个完整的堆栈
                        int fullStacks = toGainAmount / maxStackSize;
                        int remainder = toGainAmount % maxStackSize;

                        // 给予完整堆栈的物品
                        for (int i = 0; i < fullStacks; i++) {
                            ItemStack fullStack = new ItemStack(toGiveMoney, maxStackSize);
                            InventoryMethod.addItemToInventoryOrDropAll(player, fullStack);
                        }

                        // 给予剩余物品
                        if (remainder > 0) {
                            ItemStack remainderStack = new ItemStack(toGiveMoney, remainder);
                            InventoryMethod.addItemToInventoryOrDropAll(player, remainderStack);
                        }
                    }
                }
                }
        }

        return InteractionResultHolder.consume(itemInHand);
    }

    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> tooltip, TooltipFlag p_41424_) {
        super.appendHoverText(p_41421_, p_41422_, tooltip, p_41424_);

        if(!this.tooltip.get(0).getString().isEmpty())
        {
            for(Component component:this.tooltip)
            {
                tooltip.add(component);
            }
        }
    }
}